﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.ComplexClasses;
using DAL;
using App_Log;
using App_Utils;
namespace BLL
{
    public class BL_Permission
    {
        public static Permission SaveRecord(Permission objPermission)
        {
            try
            {
                clsLog.logMsg("Permission", "SaveRecord", "Started");
                DAL.ithubEntities db = new ithubEntities();
                var query = (from q in db.tblPermissions where q.client_id == objPermission.client_id
                             && q.user_id == objPermission.user_id
                             select q).SingleOrDefault();

                if (query != null)
                {
                    query = (tblPermission)clsUtils.ConvertToObject(objPermission, new tblPermission());
                    db.SaveChanges();
                }
                else
                {
                    tblPermission _objtblPermission = new tblPermission();
                    _objtblPermission = (tblPermission)clsUtils.ConvertToObject(objPermission, new tblPermission());
                    db.tblPermissions.Add(_objtblPermission);
                    db.SaveChanges();
                    objPermission = (Permission)clsUtils.ConvertToObject(_objtblPermission, objPermission);
                }
            }
            catch (Exception ex)
            {
                clsLog.logError("Permission", "SaveRecord", ex.Message);
            }
            return objPermission;
        }

        public static Permission GetPermission(long client_id,long user_id)
        {
            Permission objPermission = new Permission();

            try
            {
                clsLog.logMsg("Permission", "GetPermission", "Started");

                DAL.ithubEntities db = new ithubEntities();
                var query = (from q in db.tblPermissions where q.client_id == client_id && q.user_id == user_id select q).SingleOrDefault();

                if (query != null)
                {
                    objPermission = (Permission)App_Utils.clsUtils.ConvertToObject(query, new Permission());
                }
                else
                {
                    objPermission = null;
                }
            }
            catch (Exception ex)
            {
                clsLog.logError("Permission", "GetPermission", ex.Message);
            }
            return objPermission;
        }
    }
}
